﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(USTimelineObserver))]
public class USTimelineObserverEditor : USTimelineBaseEditor
{
    private bool hasRegisteredForUndoRedo;
    private USTimelineObserver ourObserver;

    public override void AddRightClickOptions(GenericMenu contextMenu, Vector2 mousePosition)
    {
        if (USSelection.GetNumOfSelections() <= 1)
        {
            USTimelineObserver targetTimeline = base.TargetTimeline as USTimelineObserver;
            if (targetTimeline != null)
            {
                contextMenu.AddSeparator(string.Empty);
                USObserverKeyframe keyframe = null;
                foreach (USObserverKeyframe keyframe2 in targetTimeline.observerKeyframes)
                {
                    if (USSelection.IsInUnitySelection(keyframe2))
                    {
                        keyframe = keyframe2;
                    }
                }
                Camera[] cameraArray = Resources.FindObjectsOfTypeAll(typeof(Camera)) as Camera[];
                bool flag = false;
                foreach (Camera camera in cameraArray)
                {
                    if (Path.GetExtension(AssetDatabase.GetAssetOrScenePath(camera)) == ".unity")
                    {
                        flag = true;
                        ContextData userData = new ContextData();
                        userData.selectedkeyframe = keyframe;
                        userData.observer = targetTimeline;
                        userData.camera = camera;
                        userData.time = USControl.convertEventPanePositionToTime(mousePosition.x);
                        contextMenu.AddItem(new GUIContent("Set Camera/" + camera.name), false, new GenericMenu.MenuFunction2(this.SetKeyframe), userData);
                    }
                }
                if (!flag)
                {
                    contextMenu.AddDisabledItem(new GUIContent("Please Save Your Scene, or add a camera first."));
                }
            }
        }
    }

    public override void GetSelectableObjectUnderPos(ref List<UnityEngine.Object> selectedObjects, Rect selectionArea)
    {
        USTimelineObserver targetTimeline = base.TargetTimeline as USTimelineObserver;
        foreach (USObserverKeyframe keyframe in targetTimeline.observerKeyframes)
        {
            if (keyframe != null)
            {
                float left = USControl.convertTimeToEventPanePosition(keyframe.FireTime);
                Rect rectA = new Rect(left, this.myArea.y, 6f, this.myArea.height);
                if (USEditorUtility.DoRectsOverlap(rectA, selectionArea))
                {
                    selectedObjects.Add(keyframe);
                }
            }
        }
    }

    public override void OnDrag()
    {
        USTimelineObserver targetTimeline = base.TargetTimeline as USTimelineObserver;
        targetTimeline.Process(targetTimeline.Sequence.RunningTime);
    }

    private void OnEnable()
    {
        base.Setup();
        this.myArea.height = 17f;
        if (!this.hasRegisteredForUndoRedo)
        {
            this.hasRegisteredForUndoRedo = true;
            USControl.RegisterUndoRedo(new USControl.UndoRedoFunction(this.OnUndoRedo));
        }
    }

    public override void OnFloatingContents(Rect thisArea)
    {
        thisArea.height = this.myArea.height;
        thisArea.x += 13f;
        thisArea.width -= thisArea.x;
        USTimelineBase targetTimeline = base.TargetTimeline as USTimelineBase;
        GUI.Label(thisArea, targetTimeline.name);
    }

    public override void OnTimelineBackground(Rect thisArea)
    {
        GUI.Box(thisArea, string.Empty, base.defaultTimelineBackground);
    }

    public override void OnTimelineContents(Rect thisArea)
    {
        USTimelineObserver targetTimeline = base.TargetTimeline as USTimelineObserver;
        this.ourObserver = targetTimeline;
        thisArea.height = this.myArea.height;
        base.myArea = thisArea;
        GUI.Box(thisArea, string.Empty, base.defaultTimelineBackground);
        foreach (USObserverKeyframe keyframe in targetTimeline.observerKeyframes)
        {
            if (keyframe != null)
            {
                Color color = GUI.color;
                if (USSelection.IsInUnitySelection(keyframe))
                {
                    GUI.color = Color.yellow;
                }
                float left = USControl.convertTimeToEventPanePosition(keyframe.FireTime);
                Rect position = new Rect(left, thisArea.y, 4f, thisArea.height - 1f);
                GUI.Box(position, string.Empty);
                GUI.color = color;
                position.x += position.width;
                position.width = 1000f;
                GUI.Label(position, (keyframe.camera == null) ? "Null" : keyframe.camera.name);
            }
        }
    }

    private void OnUndoRedo()
    {
        if (this.ourObserver != null)
        {
            foreach (USObserverKeyframe keyframe in this.ourObserver.observerKeyframes)
            {
                if ((keyframe != null) && (keyframe.camera != null))
                {
                    keyframe.camera.gameObject.active = false;
                }
            }
            this.ourObserver.StopTimeline();
            this.ourObserver.Process(this.ourObserver.Sequence.RunningTime);
        }
    }

    public override void ProcessRemoveObject()
    {
        if ((this.ourObserver != null) && (this.ourObserver.observerKeyframes.Count != 0))
        {
            for (int i = this.ourObserver.observerKeyframes.Count - 1; i >= 0; i--)
            {
                USObserverKeyframe selectedObject = this.ourObserver.observerKeyframes[i];
                if (USSelection.IsInUnitySelection(selectedObject))
                {
                    Undo.RegisterUndo(selectedObject.observer, "Remove From Sequence");
                    this.ourObserver.RemoveKeyframe(selectedObject);
                    this.ourObserver.StopTimeline();
                    this.ourObserver.Process(this.ourObserver.Sequence.RunningTime);
                }
            }
        }
    }

    public void ReleaseDraggingObject()
    {
        if (this.ourObserver != null)
        {
            this.ourObserver.Process(this.ourObserver.Sequence.RunningTime);
        }
    }

    private void SetKeyframe(object obj)
    {
        ContextData data = obj as ContextData;
        Undo.RegisterUndo(data.selectedkeyframe, "Set Camera");
        this.ourObserver.StopTimeline();
        if (data.selectedkeyframe == null)
        {
            Undo.RegisterUndo(data.observer, "Set Camera");
            data.observer.AddKeyframe(data.time, data.camera);
        }
        else
        {
            data.observer.SetKeyframeCamera(data.selectedkeyframe, data.camera);
        }
        this.ourObserver.Process(data.observer.Sequence.RunningTime);
    }

    private class ContextData
    {
        public Camera camera;
        public USTimelineObserver observer;
        public USObserverKeyframe selectedkeyframe;
        public float time;
    }
}

